const api = "http://localhost:3000"

function addU(){
    location.href = './add.html'
}

function filterU(){
    render()
}

function editU(id){
    location.href = `./up.html?id=${id}`
}

 function delU(id){
    if (confirm("确认删除吗？？？")) {
         axios.delete(`${api}/user/${id}`).then(()=>{
            location.href = './index.html'
         })
    }
}

render()

function render(){
    
    let keyword = $('#keyword').val()
    console.log(keyword);
    
    axios.get(`${api}/users/?keyword=${keyword}`).then((res)=>{
        
        let users = res.data.data
        console.log(users);
        $('tbody').empty()
        users.forEach(item => {
            
            $('tbody').append(
                `
                <tr>
                        <td>${item.id}</td>
                        <td>${item.uname}</td>
                        <td>${item.uage}</td>
                        <td>
                            <input type="button" value="编辑" onclick="editU(${item.id})">
                            <input type="button" value="删除" onclick="delU(${item.id})">
                        </td>
                    </tr>
                `
            )
        });
    })   
    
}